package levelrewind.android.app.adapter;

import java.util.ArrayList;

import levelrewind.android.app.R;
import levelrewind.android.app.model.PictureHandler;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.vo.HighScore;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HighScoreAdapter extends BaseAdapter {

	private ArrayList<HighScore>  highScores = new ArrayList<HighScore>();
    private LayoutInflater mInflater;
    private Context context;
    
    public HighScoreAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }
    
    public void addItem(final HighScore player) {
    	highScores.add(player);
        notifyDataSetChanged();
    }
	
	@Override
	public int getCount() {
		return highScores.size();
	}

	@Override
	public Object getItem(int position) {
		return highScores.get(position);
	}

	@Override
	public long getItemId(int position) {
		return highScores.get(position).getPersonId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null)
		{
			convertView = mInflater.inflate(R.layout.user_score_list, null);
			holder = new ViewHolder();
			holder.user_name = (TextView)convertView.findViewById(R.id.user_name);
			holder.user_score = (TextView)convertView.findViewById(R.id.user_score);
			holder.user_game = (ImageView)convertView.findViewById(R.id.user_game);
			
			holder.user_name.setTypeface(Util.customFont(context));
			holder.user_score.setTypeface(Util.customFont(context));
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.user_name.setText(highScores.get(position).getUser().getName());
		String score = Util.formatNumber(highScores.get(position).getScore());
		holder.user_score.setText(score);
		
		PictureHandler ph = new PictureHandler(context);
		holder.user_game.setImageDrawable(ph.getPictureFromDevice(highScores.get(position).getGame().getName()));
		
		return convertView;
	}
	
	public static class ViewHolder {
        public TextView user_name;
        public TextView user_score;
        public ImageView user_game;
    }

}
